package ro.imc.ums.dani.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUI {

	/*
	 * Welcome Window
	 */
	private JFrame window;
	private JPanel panel;
	private JButton exit;
	private JButton register;
	private JButton login;
	private JLabel usernameLabel;
	private JLabel passwordLabel;
	private JTextField usernameInput;
	private JTextField passwordInput;

	/*
	 * Register Window
	 */
	private JFrame registerWindow;
	private JPanel registerPanel;
	private JButton registerUserBtn;
	private JButton registerCloseBtn;
	private JTextField username;
	private JTextField password;
	private JComboBox accountType;
	private JLabel accountTypeLabel;
	private JComboBox type;
	private int accountStatus;
	
	public GUI() {
		createInterface();
		createPanel();
		createWelcomeWindow();
		window.setVisible(true);
	}

	private void createInterface() {
		window = new JFrame();
		window.setTitle("UMS");
		window.setSize(500, 400);
		window.setLocation(300, 300);
		window.setResizable(false);
		window.setLayout(new BorderLayout());
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void createPanel() {
		panel = new JPanel();
		panel.setLayout(null);
		panel.setBackground(Color.WHITE);
		window.add(panel);
	}

	/*
	 * Create Welcome Window
	 */
	private void createWelcomeWindow() {
		exit = new JButton("Exit");
		exit.setSize(85, 25);
		exit.setLocation(360, 325);
		exit.setBackground(Color.red);

		ActionListener actionExit = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		};
		exit.addActionListener(actionExit);

		register = new JButton("Register");
		register.setSize(85, 25);
		register.setLocation(265, 325);
		register.setBackground(Color.green);
		register.setEnabled(false);
		ActionListener actionRegister = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				createRegisterWindow();
			}
		};
		register.addActionListener(actionRegister);

		usernameLabel = new JLabel("Username");
		usernameLabel.setSize(150, 20);
		usernameLabel.setLocation(40, 50);

		usernameInput = new JTextField();
		usernameInput.setSize(150, 20);
		usernameInput.setLocation(40, 80);

		passwordLabel = new JLabel("Password");
		passwordLabel.setSize(150, 20);
		passwordLabel.setLocation(40, 120);

		passwordInput = new JTextField();
		passwordInput.setSize(150, 20);
		passwordInput.setLocation(40, 150);
		
		String[] index = { "Admin", "Professor", "Student" };
		type = new JComboBox(index);
		type.setLocation(250,80);
		
		type.setSize(110, 20);
		type.setBackground(Color.white);
		ActionListener actionStatus = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox) e.getSource();
				String x = (String) cb.getSelectedItem();
				if (x == "Admin") {
					accountStatus = 1;
				} else if (x == "Professor") {
					accountStatus = 2;
				} else if (x == "Student") {
					accountStatus = 3;
				}
			}
		};
		type.addActionListener(actionStatus);
		type.setSelectedIndex(0);
		
		login = new JButton("Login");
		login.setSize(85, 25);
		login.setLocation(40, 190);
		login.setBackground(Color.cyan);

		ActionListener actionLogin = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				window.dispose();
				if(accountStatus == 1){
					AdminInterface ai = new AdminInterface();
				} else if(accountStatus == 2){
					ProfessorInterface pi = new ProfessorInterface();
					pi.professorInformation(usernameInput.getText());
				} else if(accountStatus == 3){
					StudentInterface si = new StudentInterface();
					si.studentInformation(usernameInput.getText());
				}
				
				
			}

		};

		login.addActionListener(actionLogin);

		panel.add(usernameLabel);
		panel.add(usernameInput);
		panel.add(passwordLabel);
		panel.add(passwordInput);
		panel.add(type);
		panel.add(login);
		panel.add(register);
		panel.add(exit);
	}

	/*
	 * Register User Window
	 */
	private void createRegisterWindow() {
		registerWindow = new JFrame();
		registerWindow.setTitle("Register Account");
		registerWindow.setSize(300, 300);
		registerWindow.setLocation(300, 300);
		registerWindow.setResizable(false);
		registerWindow.setLayout(new BorderLayout());
		registerWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		registerWindow.setVisible(true);

		registerPanel = new JPanel();
		registerPanel.setLayout(null);
		registerPanel.setBackground(Color.white);
		registerWindow.add(registerPanel);

		usernameLabel = new JLabel("Username");
		usernameLabel.setSize(150, 20);
		usernameLabel.setLocation(20, 10);

		username = new JTextField();
		username.setSize(150, 20);
		username.setLocation(20, 30);

		passwordLabel = new JLabel("Password");
		passwordLabel.setSize(150, 20);
		passwordLabel.setLocation(20, 70);

		password = new JTextField();
		password.setSize(150, 20);
		password.setLocation(20, 90);

		accountTypeLabel = new JLabel("Account Type");
		accountTypeLabel.setSize(150, 20);
		accountTypeLabel.setLocation(20, 130);

		String[] index = { "Student", "Professor" };
		accountType = new JComboBox(index);
		accountType.setSelectedIndex(-1);
		accountType.setSize(150, 20);
		accountType.setLocation(20, 160);
		accountType.setBackground(Color.white);

		ActionListener actionAccountType = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

			}

		};

		registerUserBtn = new JButton("Register");
		registerUserBtn.setSize(90, 25);
		registerUserBtn.setLocation(45, 230);
		registerUserBtn.setBackground(Color.green);

		ActionListener actionRegisterUserBtn = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				registerWindow.dispose();
				window.setVisible(true);
			}

		};

		registerCloseBtn = new JButton("Cancel");
		registerCloseBtn.setSize(90, 25);
		registerCloseBtn.setLocation(155, 230);
		registerCloseBtn.setBackground(Color.red);

		ActionListener actionRegisterCloseBtn = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				registerWindow.dispose();
				window.setVisible(true);
			}

		};

		accountType.addActionListener(actionAccountType);
		registerUserBtn.addActionListener(actionRegisterUserBtn);
		registerCloseBtn.addActionListener(actionRegisterCloseBtn);
		registerPanel.add(usernameLabel);
		registerPanel.add(passwordLabel);
		registerPanel.add(accountTypeLabel);
		registerPanel.add(accountType);
		registerPanel.add(username);
		registerPanel.add(password);
		registerPanel.add(registerUserBtn);
		registerPanel.add(registerCloseBtn);

	}

}
